using System;
using System.Collections.Generic;
using System.Text;
using SharpMap.Presentation.Views;
using System.ComponentModel;
using SharpMap.Layers;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
using System.Collections;

namespace SharpMap.Presentation.Wpf
{

    /// <summary>
    /// 
    /// </summary>
    public class LayerListViewModel : INotifyPropertyChanged
    {
        Map _map;
        ILayersView _view;
        public Map Map
        {
            get { return _map; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="map">Map to present.</param>
        /// <param name="view">View to present to.</param>
        public LayerListViewModel(Map map, ILayersView view)
        {
            _map = map;
            _map.Layers.ListChanged += layersListChanged;
            _view = view;
            _view.LayersSelectionChangeRequested += viewLayersSelectionChangeRequested;
            Layers = _map.Layers;
           
        }

        public ILayer SelectedLayer
        {
            get 
            {
                if (_map != null && _map.SelectedLayers.Count != 0)
                {
                    return _map.SelectedLayers[0];
                }
                return null;
            }
        }

        void layersListChanged(object sender, ListChangedEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.ItemChanged)
            {
                if (e.PropertyDescriptor.Name == Layer.EnabledProperty.Name
                    || e.PropertyDescriptor.Name == Layer.StyleProperty.Name)
                {
                    RaisePropertyChanged(Layer.EnabledProperty.Name);
                    RaisePropertyChanged("SelectedLayer");
                }
            }
        }

        void viewLayersSelectionChangeRequested(object sender, LayerActionEventArgs e)
        {
            switch (e.LayerAction)
            {
                case LayerAction.Enabled:
                    break;
                case LayerAction.Disabled:
                    break;
                case LayerAction.Selected:
                    foreach (string layerName in e.Layers)
                    {
                        Map.SelectLayer(layerName);
                    }
                    break;
                case LayerAction.Deselected:
                    foreach (string layerName in e.Layers)
                    {
                        Map.DeselectLayer(layerName);
                    }
                    break;
                default:
                    break;
            }
            RaisePropertyChanged("SelectedLayer");

        }



        //TODO: this is temporary. should be collectionView
        public IList Layers
        {
            get { return _layers; }
            set { _layers = value; RaisePropertyChanged("Layers"); }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                PropertyChangedEventArgs args =
                    new PropertyChangedEventArgs(propertyName);
                handler(this, args);
            }
        }
        #endregion

        IList _layers;
    }
        
    
}
